﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using DAL;
using Microsoft.Practices.Prism.Events;
using TripAssistantDesktop.TripAssistantWCFService;

namespace TripAssistantDesktop.ViewModel
{
    internal interface IOrdersViewModel
    {
         IList<Order> DataSource { get; set;  }
    }

    class OrdersViewModel : ViewModelBase, IOrdersViewModel
    {
        private ObservableCollection<Order> _orders = new ObservableCollection<Order>();
        private IEventAggregator _eventAggregator;
        private IAssistantService _srv;  
        public ObservableCollection<Order> Orders 
        {
            get { return _orders; }
            set { _orders = value; }
        }

        public IList<Order> DataSource
        {
            get { return _dataSource; }
            set
            {
                _orders.Clear();
                value.ToList().ForEach(x=> _orders.Add(x));
                
            }
        }

        public OrdersViewModel(IEventAggregator eventAggregator, IAssistantService srv)
        {
            _srv = srv; 
            //_srv.GetOrders(new Trip()).ToList().ForEach(x => _orders.Add(x)); 
            _eventAggregator = eventAggregator;
            SelectedTripChangedEvent tripChangedEvent = eventAggregator.GetEvent<SelectedTripChangedEvent>();
            var subscriptionToken = tripChangedEvent.Subscribe(TripChangedEventHandler, ThreadOption.UIThread, false, TripChangedFilter);
        }

        private bool TripChangedFilter(Trip obj)
        {
            return true; 
            throw new NotImplementedException();
        }

        private void TripChangedEventHandler(Trip obj)
        {
            DataSource = _srv.GetOrders(obj); 
            //throw new NotImplementedException();
        }

        private IList<Order> _dataSource; 
       // private IAssistantService _srv = new AssistantAssistantService();
        
    }
}
